<?php
	//exemple de fil d'ariane
	$ariane = array(
		array(
			'href' => "index.php",
			'name' => "Home"
		)
	);
	
	require("include/includes.php");
	/*
		init de twig déplacé dans './include/lib/startTwig.lib.php' appelé dans includes.php
	*/
	$template = $twig->loadTemplate('index.html');
	
	
	if(!isset($_SESSION["user"]))
		$userId = 0;
	else
		$userId = $_SESSION["user"]->getId();
		
	// -------------------------- NEWSFEED --------------------------
	
	// Valeurs initiales
	$begin = 0;
	if(isset($_GET['begin']) && is_numeric($_GET['begin']))
	{
		$begin=(int)$_GET['begin'];
	}
	$number = 20;
	if(isset($_GET['number']) && is_numeric($_GET['number']))
	{
		$number=(int)$_GET['number'];
	}
	
	// Récupération dans la bdd
	// RecordsHistory
	$news = RecordsHistory::getAll($begin, $number);
	$newsArray = Array();
	foreach($news as $element)
	{
		//$date = new DateTime ($element->getDate());
		//$formattedDate = $date->format ('m/d/Y H:i');
		$formattedDate = time_since(strtotime($element->getDate())).' ago';
		$newsArray[] = Array(
				'id' => (int)$element->getId(),
				'playerId' => (int)$element->getPlayer()->getId(),
				'playerName' => $element->getPlayer()->getNickName(),
				'trackType' => $element->getTrackType(),
				'trackName' => $element->getTrackName(),
				'time' => Record::timeToString($element->getTime(), false, true),
				'rank' => (int)$element->getRank(),
				'mode' => $element->getTrackMode(),
				'version' => $element->getVersion(),
				//'character' => $element->,
				'date' => $formattedDate,
				'action' => $element->getAction()
			);
	}
	// ------------------------------------------------------------------------
		
	$template->display(array(
		'pageTitle' => 'Index',
		'meta_description' => 'The site of all records on Crash Team Racing trial time !',
		'userId' => $userId,
		'news' => $newsArray
	));

?>